package backend_agent

import (
	"context"
	"xialou_platform/app/requestx"
	"xialou_platform/services/agent/rpc/agentClient"
	"xialou_platform/services/wallet/rpc/walletClient"

	"xialou_platform/services/agent/api/internal/svc"
	"xialou_platform/services/agent/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProvinceDataLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewProvinceDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) ProvinceDataLogic {
	return ProvinceDataLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProvinceDataLogic) ProvinceData() (resp *types.ProvinceDataResp, err error) {

	agentId, err := requestx.FetchUserIdByJwt(l.ctx)
	if err != nil {
		return nil, err
	}

	profit, err := l.svcCtx.WalletRpc.AgentProfit(l.ctx, &walletClient.AgentProfitReq{
		AgentId: agentId,
	})
	if err != nil {
		return nil, err
	}
	agentData, err := l.svcCtx.AgentRpc.AgentData(l.ctx, &agentClient.AgentDataReq{
		AgentId: agentId,
	})
	resp = &types.ProvinceDataResp{
		AgentNum:     agentData.UserNum,
		CityAgentNum: 0,
		StoreNum:     agentData.StoreNum,
		CardProfit:   profit.CardProfit,
		GroupProfit:  profit.GroupProfit,
		PayProfit:    profit.PayProfit,
		TotalProfit:  profit.CardProfit + profit.GroupProfit + profit.PayProfit,
	}

	return
}
